/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

import { TaskNames } from '../common/task-names.js';
import { TargetTaskService } from '../service/target-task-service.js';
import { OhosHapTask } from '../task/ohos-hap-task.js';
import CommonTask = TaskNames.CommonTask;
import * as fse from 'fs-extra';
import path from 'path';

import { CommonConst, UnitTestConst } from '../../const/common-const.js';
import { ModuleJson } from '../../options/configure/module-json-options.js';
import { getJson5Obj } from '../../utils/json-util.js';
import { ProcessProfile } from '../process-profile.js';

/**
 * UnitTestBuild场景处理构建中间产物module.json文件
 * 1.增加TestAbility入口
 *
 * @since 2023/9/11
 */
export class UnitTestProcessProfile extends OhosHapTask {
  private readonly processProfileModuleJsonPath: string;

  constructor(taskService: TargetTaskService) {
    super(taskService, CommonTask.UNIT_TEST_PROCESS_PROFILE);
    this.processProfileModuleJsonPath = this.pathInfo.getIntermediatesProcessProfile();
  }

  initTaskDepends(): void {
    this.declareDepends(ProcessProfile.name);
  }

  protected doTaskAction() {
    const testAbilityPath: string = path.resolve(this.pathInfo.getModuleBuildPath(), UnitTestConst.TEST_ABILITY_ETS_PATH);
    if (!fse.existsSync(testAbilityPath)) {
      return;
    }

    const moduleJsonObj: ModuleJson.ModuleOptObj = getJson5Obj(this.processProfileModuleJsonPath);
    let abilities: ModuleJson.AbilityObj[] | undefined = moduleJsonObj?.module?.abilities;
    const moduleModel = this.targetService.getModuleService().getModuleModel();

    if (!abilities?.length) {
      if (moduleModel.isHspModule() || moduleModel.isHarModule()) {
        abilities = moduleJsonObj.module.abilities = [];
      } else {
        return;
      }
    }

    if (moduleModel.isHspModule() || moduleModel.isHarModule()) {
      moduleJsonObj.module.deliveryWithInstall = moduleModel.isHarModule();
      if (moduleModel.isHarModule()) {
        moduleJsonObj.module.type = 'entry';
        moduleJsonObj.app.icon = CommonConst.ICON;
        moduleJsonObj.app.label = CommonConst.LABEL;
      }
    }

    const { startWindowIcon, startWindowBackground } = abilities?.[0] || {
      startWindowIcon: 'startIcon',
      startWindowBackground: '#FFFFFF',
    };
    const testAbilityObj: ModuleJson.AbilityObj = {
      name: UnitTestConst.TEST_ABILITY,
      srcEntrance: UnitTestConst.ENTRY_TEST_ABILITY_PATH,
      startWindowIcon: startWindowIcon,
      startWindowBackground: startWindowBackground,
    };

    abilities.push(testAbilityObj);
    fse.outputJSONSync(this.processProfileModuleJsonPath, moduleJsonObj, { spaces: '\t' });

    // ark编译aceModuleJsonPath更改为ark_module.json,此处额外处理ark_module.json
    // 1.补齐testAbility;2.补齐main_page.json
    Object.assign(moduleJsonObj.module,{
      pages: "$profile:main_pages"
    })
    fse.outputJSONSync(this.pathInfo.getIntermediatesArkModuleJsonPath(), moduleJsonObj, { spaces: '\t' });
  }
}
